<template>
  <a-drawer width="60%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }" :model="form" :rules="rules">
      <a-form-model-item label="配送类型" prop="type">
        <a-select v-model="form.type" placeholder="请输入配送类型" allow-clear>
          <a-select-option v-for="(d, index) in deliveryOp" :key="index" :value="d.dictValue">{{ d.dictLabel
            }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="选择会员卡">
        <a-select v-model="form.card" placeholder="请选择卡类型" allow-clear @change="cardChange">
          <a-select-option v-for="(d, index) in cardOp" :key="index" :value="d.id">{{ d.name }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="会员卡信息" prop="count">
        <a-descriptions :column="1">
          <a-descriptions-item label="卡类型">
            {{ cardInfo.name }}
          </a-descriptions-item>
          <a-descriptions-item label="总次数">
            {{ cardInfo.count }}
          </a-descriptions-item>
        </a-descriptions>
      </a-form-model-item>
      <a-form-model-item label="剩余次数" prop="remain">
        <a-input v-model="form.remain" placeholder="请输入金额" />
      </a-form-model-item>
      <a-form-model-item label="金额" prop="amount">
        <a-input v-model="form.amount" placeholder="请输入金额" />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark">
        <a-input v-model="form.remark" placeholder="请输入内容" type="textarea" allow-clear />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getMemberMilkCard, addMemberMilkCard, updateMemberMilkCard } from '@/api/system/memberMilkCard'
import { getMilkDictOp } from '@/api/system/milkDict'
import { getCardOp } from '@/api/system/milkCard'
export default {
  name: 'CreateForm',
  props: {
  },
  components: {
  },
  data() {
    return {
      cardOp: [],
      deliveryOp: [],
      submitLoading: false,
      formTitle: '',
      // 表单参数
      cardInfo:{
        name:'',
        count:null
      },
      form: {
        card:{},
        id: null,

        type: null,

        count: null,

        milkType: null,

        remain: null,

        amount: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
      }
    }
  },
  filters: {
  },
  created() {
    this.getCardList()
  },
  computed: {
  },
  watch: {
  },
  mounted() {
  },
  methods: {
    cardChange(value, option){
      this.cardInfo = this.cardOp[option.key]
      this.form.count = this.cardInfo.count
      this.form.remain = this.cardInfo.count
    },
    getCardList() {
      getCardOp().then(resp => {
        this.cardOp = resp.data
      })
    },
    getDictList(code) {
      getMilkDictOp({ code: code }).then(resp => {
        this.deliveryOp = resp
      })
    },
    onClose() {
      this.open = false
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.formType = 1
      this.form = {
        id: null,

        type: null,

        count: null,

        milkType: null,

        remain: null,

        amount: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null

      }
    },
    /** 新增按钮操作 */
    handleAdd(id) {
      this.getDictList('Delivery')
      this.reset()
      this.form.member = id
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate(row, ids) {
      this.getDictList('Delivery')
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getMemberMilkCard(id).then(response => {
        this.form = response.data
        this.cardInfo.name = this.form.cardName
        this.cardInfo.count = this.form.count;
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateMemberMilkCard(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addMemberMilkCard(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
